//
//  NLHomeCell.swift
//  sinaWeibo7110
//
//  Created by 安静清晨 on 2017/1/10.
//  Copyright © 2017年 walk-in-minds. All rights reserved.
//

import UIKit
import SDWebImage

//左右页边距
let homeMargin : CGFloat = 8
//插图视图的最大宽度
let maxWidth : CGFloat = ScreenWidth - homeMargin * 4
//定义两张插图之间的间距
let picMargin : CGFloat = 2
//插图视图的单个宽度
let itemWith : CGFloat = (maxWidth - picMargin * 2) / 3




class NLHomeCell: UITableViewCell {

    var count : Int = 0
    
    //用户头像
    @IBOutlet weak var headIcon: UIImageView!
    
    //用户昵称
    @IBOutlet weak var screen_name: UILabel!
    
    //发布时间
    @IBOutlet weak var created_at: UILabel!
    
    //发布的文字内容
    @IBOutlet weak var content: UILabel!

    //微博来源
    @IBOutlet weak var source: UILabel!
    
    //认证类型
    @IBOutlet weak var verified_type: UIImageView!
    
    //会员等级
    @IBOutlet weak var mbrank: UIImageView!
    
    //插图视图组
    @IBOutlet weak var picCollectionView: NLHomePicCollectionView!
    
    //插图视图组流水布局
    @IBOutlet weak var picFlowLayout: UICollectionViewFlowLayout!
    
    //插图视图的宽高约束
    @IBOutlet weak var picHeightCons: NSLayoutConstraint!
    @IBOutlet weak var picWidthCons: NSLayoutConstraint!
    
    @IBOutlet weak var picTopCons: NSLayoutConstraint!
    
    //底端工具条
    @IBOutlet weak var toolBar: UIStackView!
    
    //被转发微博的微博作者
    
    @IBOutlet weak var retweeted_userName: UILabel!
    
    //被转发微博的正文
    
    @IBOutlet weak var retweeted_text: UILabel!
    
    
    var homeCellViewModel = NLHomeCellViewModel() {
        
        didSet{
            
            //判断是否为转发微博
            if homeCellViewModel.statuses?.retweeted_status != nil {
                
                retweeted_userName.text = "@" + (homeCellViewModel.statuses?.retweeted_status?.user?.screen_name ?? "")
                retweeted_text.text = homeCellViewModel.statuses?.retweeted_status?.text
                
                picCollectionView.pic_urls = homeCellViewModel.statuses?.retweeted_status?.pic_urls
                let count = homeCellViewModel.statuses?.retweeted_status?.pic_urls?.count
                self.count = count ?? 0
                
            }else{
                //插图视图组的数据源数据(原创微博)
                picCollectionView.pic_urls = homeCellViewModel.statuses?.pic_urls
                let count = homeCellViewModel.statuses?.pic_urls?.count
                self.count = count ?? 0
            }

            //视图组的宽高约束
            
            let picWH = getCollectionViewWH(count: self.count)
            picHeightCons.constant = picWH.height
            picWidthCons.constant = picWH.width
            
            //如果没有插图. 插图距离正文的约束就取消掉.
            picTopCons.constant = (count == 0 ? 0 : 8)
            
            self.screen_name.text = homeCellViewModel.statuses?.user?.screen_name
            
            let urlString = homeCellViewModel.statuses?.user?.avatar_large
            let url = URL(string: urlString!)
            self.headIcon.sd_setImage(with: url)
            
            self.created_at.text = homeCellViewModel.time
            self.content.text = homeCellViewModel.statuses?.text
            //flag
            self.source.text = homeCellViewModel.source
            self.mbrank.image = homeCellViewModel.mbrankImage
            self.verified_type.image = homeCellViewModel.verified_typeImage
            
            
        }
        
    }
    

    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        content.numberOfLines = 0
        content.preferredMaxLayoutWidth = ScreenWidth - 2 * 8
        
        picCollectionView.backgroundColor = UIColor.white
//        picFlowLayout.itemSize = CGSize(width: itemWith, height: itemWith)
        //设置间距
        picFlowLayout.minimumLineSpacing = picMargin
        picFlowLayout.minimumInteritemSpacing = picMargin
        
        
        
    }


    //由图片张数来推导整个插图视图组的宽高
    private func getCollectionViewWH (count:Int) -> CGSize{
        //只有当单张图片时, 改变布局.
        picFlowLayout.itemSize = CGSize(width: itemWith, height: itemWith)
        
        
        if count == 0 {
            
            return CGSize.zero
        }
        //String:self.homeCellViewModel.picInfos?.last?.bmiddle ?? ""
        
        if count == 1{
            //暂时
//            return CGSize(width: itemWith, height: itemWith)
            
            let url = URL(string: self.homeCellViewModel.picInfos?.last?.wap360 ?? "")
            print("转发单张")
            //使用SDWebImage方法下载好的图片的存储地址-->获取图片
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: url?.absoluteString)
        
            if let img = image{
                
                var size = img.size
            
                //如果图片宽度超出了屏幕宽度, 则等同于屏幕宽度
                size.width = (size.width > ScreenWidth ? ScreenWidth : size.width)
                
                picFlowLayout.itemSize = size
                return size
            }
            
        }
        
        if count == 4 {
            
            let WH = itemWith * 2.01 + picMargin
            return CGSize(width: WH, height: WH)
        }
        
        //其他. 先计算行数.
        let row : Int = (count - 1)/3 + 1
        
        let H = itemWith * CGFloat(row) + (CGFloat(row) - 1) * picMargin
        
        return CGSize(width: maxWidth, height: H)
    }
    
    
    
    
    
    
    
    
    
}
